package com.leyou.common.utils;

import com.leyou.common.constants.CommonFileConstant;
import com.leyou.common.vo.UploadFileVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import java.util.List;


/**
 * 类名称：UploadFileUtils
 * 类描述：UploadFileUtils
 * 文件上传工具类
 * 创建人：wgc 创
 * 建时间：2018-03-31
 * 上午12:44:20
 */
public class UploadFileUtils {
    // 文件允许格式
    private static String[] allowFiles = {"jpg", "png", "txt", "doc", "docx", "xls", "xlsx", "pdf", "rar", "zip"};
    private static Logger log = LoggerFactory.getLogger(UploadFileUtils.class);
    // 最大500M
    static long maxSize = 1024 * 1024 * 500;

    /**
     * @param request
     * @return
     * @throws Exception
     */
    public static List<UploadFileVO> uploadFile(HttpServletRequest request) throws Exception {
        long startTime = System.currentTimeMillis();   //获取开始时间
        List<UploadFileVO> list = new ArrayList<UploadFileVO>();
        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        String filePath = "";
        String fileName = "";// 文件名称
        FileOutputStream fos = null;
        UploadFileVO file = null;
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
                multiRequest.getSession().getServletContext());
        if (multipartResolver.isMultipart(multiRequest)) {
            log.info("{******************开始上传**********************}");
            Iterator<String> iter = multiRequest.getFileNames();
            while (iter.hasNext()) {
                List<MultipartFile> multipartFiles = multiRequest.getFiles(iter.next());
                if (multipartFiles != null && !multipartFiles.isEmpty()) {
                    for (MultipartFile multipartFile : multipartFiles) {
                        file = new UploadFileVO();
                        fileName = multipartFile.getOriginalFilename();// 获取上传的文件名字
                        String suffixName = multipartFile.getOriginalFilename()
                                .substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1);
                        String num = RandomUtils.number(5);
                        filePath = File.separator + DateUtils.nowDayTimeStamp() + File.separator + num + File.separator
                                + fileName;
                        String fileUrl = null;
                        if (multipartFile.getSize() > maxSize) {
                            log.info("{******************文件超过500M**********************}");
                            return null;
                        }
                        if (PlatformUtils.isWindows()) {
                            log.info("**********Windows**************");
                            fileUrl = CommonFileConstant.WINDOWS_FILE_URL + filePath;
                            log.info("**********Windows**************" + fileUrl);
                        } else if (PlatformUtils.isLinux()) {
                            log.info("**********Linux**************");
                            fileUrl = CommonFileConstant.LINUX_FILE_URL + filePath;
                            log.info("**********Linux**************" + fileUrl);
                        }

                        try {
                            File objFile = new File(fileUrl); // 创建对象
                            if (!objFile.exists()) {
                                objFile.mkdir();
                                log.info("**********创建当前文件成功**************");
                            } else {
                                log.info(fileUrl + "====文件夹已存在");
                            }
                            // 上级文件是否存在
                            // 判断目标文件所在的目录是否存在
                            if (!objFile.getParentFile().exists()) {
                                // 如果目标文件所在的目录不存在，则创建父目录
                                objFile.getParentFile().mkdirs();
                                log.info("**********创建上级文件成功**************");
                            }
                            multipartFile.transferTo(objFile);
                            log.info("**********上传成功**************");
                            file.setFileName(fileName);
                            file.setFileSize(String.valueOf(multipartFile.getSize()));
                            file.setProjectFileUrl(filePath);
                            file.setSuffixName(suffixName);
                            list.add(file);
                        } catch (IOException ie) {
                            log.error("------------上传异常--------------" + ie.getMessage());
                            return list;
                        } finally {
                            // 关闭FileOutputStream对象
                            try {
                                if (fos != null) {
                                    fos.close();
                                }
                            } catch (Exception e) {
                                e.getMessage();
                                e.printStackTrace();
                            }
                        }
                    }

                }

            }
            long endTime = System.currentTimeMillis(); //获取结束时间
            log.info("程序运行时间： " + (endTime - startTime) + "毫秒");
            log.info("上传的文件:【" + list.size() + "】");
            return list;
        }
        return list;
    }

    /**
     * @param request
     * @return
     * @throws Exception
     */
    public static UploadFileVO uploadFileOne(HttpServletRequest request, boolean icon) throws Exception {
        long startTime = System.currentTimeMillis();   //获取开始时间
        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        String filePath = "";
        String fileName = "";// 文件名称
        FileOutputStream fos = null;
        UploadFileVO file = null;
        String fileUrl = null;

        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
                multiRequest.getSession().getServletContext());
        if (multipartResolver.isMultipart(multiRequest)) {
            log.info("{******************开始上传**********************}");
            Iterator<String> iter = multiRequest.getFileNames();
            while (iter.hasNext()) {
                List<MultipartFile> multipartFiles = multiRequest.getFiles(iter.next());
                if (multipartFiles != null && !multipartFiles.isEmpty()) {
                    for (MultipartFile multipartFile : multipartFiles) {
                        file = new UploadFileVO();
                        fileName = multipartFile.getOriginalFilename();// 获取上传的文件名字
                        String uniqueImageName = RandomUtils.genImageName();
                        String uuidImage = RandomUtils.getUUID();
                        String suffixName = multipartFile.getOriginalFilename()
                                .substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1);
                        // 如果是图片
                        if (suffixName.toLowerCase().equals("jpg") || suffixName.toLowerCase().equals("png") || suffixName.toLowerCase().equals("blob")) {
                            filePath = File.separator + DateUtils.nowDayTimeStamp() + File.separator + uniqueImageName
                                    + File.separator + uniqueImageName + ".jpg";
                        } else {
                            // 如果是文件
                            filePath = File.separator + DateUtils.nowDayTimeStamp() + File.separator + uniqueImageName
                                    + File.separator + fileName;
                        }
                        if (multipartFile.getSize() > maxSize) {
                            log.info("{******************文件超过500M**********************}");
                            return null;
                        }
                        // 判断文件格式是否包含规定的文件格式
                        if (PlatformUtils.isWindows()) {
                            log.info("**********Windows**************");
                            fileUrl = CommonFileConstant.WINDOWS_FILE_URL + filePath;
                            log.info("**********Windows**************" + fileUrl);

                        } else if (PlatformUtils.isLinux()) {
                            log.info("**********Linux**************");
                            fileUrl = CommonFileConstant.LINUX_FILE_URL + filePath;
                            log.info("**********Linux**************" + fileUrl);
                        }

                        log.info("-------------------文件创建开始-------------------");
                        try {
                            File objFile = new File(fileUrl); // 创建对象
                            if (!objFile.exists()) {
                                objFile.mkdir();

                            } else {
                                log.info(fileUrl + "====文件夹已存在");
                            }
                            // 上级文件是否存在
                            // 判断目标文件所在的目录是否存在
                            if (!objFile.getParentFile().exists()) {
                                // 如果目标文件所在的目录不存在，则创建父目录
                                objFile.getParentFile().mkdirs();
                                log.info("**********创建上级目录成功**************" + fileUrl);

                            }
                            multipartFile.transferTo(objFile);
                            log.info("-------------------文件创建结束-------------------");
                            log.info("**********上传成功**************");
                            if (checkFileType(suffixName)) {

                                // 如果不是pdf等其他格式则上传的是图片 并且有缩略图
                                if (icon && (suffixName.toLowerCase().equals("jpg") || suffixName.toLowerCase().equals("png") || suffixName.toLowerCase().equals("blob"))) {
                                    PicThumbnailUtils ptu = new PicThumbnailUtils(fileUrl);
                                    String thumbnailUrl = null;
                                    String thumbnailPath = File.separator + DateUtils.nowDayTimeStamp() + File.separator
                                            + uniqueImageName + File.separator + uuidImage + ".jpg";
                                    if (PlatformUtils.isWindows()) {
                                        log.info("**********Windows**************");
                                        thumbnailUrl = CommonFileConstant.WINDOWS_FILE_URL + thumbnailPath;
                                    } else if (PlatformUtils.isLinux()) {
                                        log.info("**********Linux**************");
                                        thumbnailUrl = CommonFileConstant.LINUX_FILE_URL + thumbnailPath;

                                    }
                                    ptu.resize(200, 200, thumbnailUrl);
                                    log.info("**********上传成功**************");
                                    file.setFileName(fileName);
                                    file.setFileSize(String.valueOf(multipartFile.getSize()));
                                    file.setProjectFileUrl(filePath);// 原图路径
                                    file.setThumbnailUrl(thumbnailPath);// 缩略图路径
                                    file.setSuffixName(suffixName);
                                } else {
                                    log.info("**********文件上传成功**************");
                                    file.setFileName(fileName);
                                    file.setFileSize(String.valueOf(multipartFile.getSize()));
                                    file.setProjectFileUrl(filePath);// 文件路径
                                    file.setSuffixName(suffixName);
                                }
                            } else {
                                //上传关于app版本管理的 apk文件
                                log.info("**********文件上传成功**************");
                                file.setFileName(fileName);
                                file.setFileSize(String.valueOf(multipartFile.getSize()));
                                file.setProjectFileUrl(filePath);// 文件路径
                                file.setSuffixName(suffixName);
                            }

                        } catch (IOException ie) {
                            log.error("------------上传异常--------------" + ie.getMessage());
                            return file;
                        } finally {
                            // 关闭FileOutputStream对象
                            try {
                                if (fos != null) {
                                    fos.close();
                                }
                            } catch (Exception e) {
                                e.getMessage();
                                e.printStackTrace();
                            }
                        }

                    }

                }

            }
            long endTime = System.currentTimeMillis(); //获取结束时间
            log.info("程序运行时间： " + (endTime - startTime) + "毫秒");
            log.info("上传的文件:【" + file + "】");
            return file;
        }
        return file;
    }

    /**
     * @param request
     * @return
     * @throws Exception
     */
    public static UploadFileVO uploadImageForApp(HttpServletRequest request) throws Exception {
        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        String filePath = null;
        String fileName = "";// 文件名称
        String flag = "mp4";
        FileOutputStream fos = null;
        UploadFileVO file = null;
        String fileUrl = null;
        String video = "video";
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
                multiRequest.getSession().getServletContext());
        if (multipartResolver.isMultipart(multiRequest)) {
            log.info("{******************开始上传**********************}");
            Iterator<String> iter = multiRequest.getFileNames();
            while (iter.hasNext()) {
                List<MultipartFile> multipartFiles = multiRequest.getFiles(iter.next());
                if (multipartFiles != null && !multipartFiles.isEmpty()) {
                    for (MultipartFile multipartFile : multipartFiles) {
                        file = new UploadFileVO();
                        fileName = multipartFile.getOriginalFilename();// 获取上传的文件名字
                        String suffixName = multipartFile.getOriginalFilename()
                                .substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1);
                        String uniqueImageName = RandomUtils.genImageName();
                        String uuidImage = RandomUtils.getUUID();
                        if (multipartFile.getSize() > maxSize) {
                            log.info("{******************文件超过500M**********************}");
                            return null;
                        }
                        // 视频文件路径
                        if (flag.equals(suffixName.toLowerCase())) {
                            log.info("{----------------视频-------------------}");
                            filePath = File.separator + DateUtils.nowDayTimeStamp() + File.separator + video
                                    + File.separator + fileName;
                            log.info("{----------------视频路径-------------------}" + filePath);
                        } else {
                            log.info("{----------------图片-------------------}");
                            // 图片原路径
                            filePath = File.separator + DateUtils.nowDayTimeStamp() + File.separator + uniqueImageName
                                    + File.separator + uniqueImageName + ".jpg";
                            log.info("{----------------图片路径-------------------}" + filePath);
                        }

                        log.info("{----------------创建文件开始------------------}");
                        if (PlatformUtils.isWindows()) {
                            log.info("**********Windows**************");
                            fileUrl = CommonFileConstant.WINDOWS_FILE_URL + filePath;
                            log.info("**********Windows**************" + fileUrl);
                        } else if (PlatformUtils.isLinux()) {
                            log.info("**********Linux**************");
                            fileUrl = CommonFileConstant.LINUX_FILE_URL + filePath;
                            log.info("**********Linux**************" + fileUrl);
                        }
                        try {
                            File objVideoFile = new File(fileUrl); // 创建对象
                            if (!objVideoFile.exists()) {
                                objVideoFile.mkdir();
                                log.info("**********创建当前目录成功**************");
                            } else {
                                log.info(fileUrl + "====文件夹已存在");
                            }
                            // 上级文件是否存在
                            // 判断目标文件所在的目录是否存在
                            if (!objVideoFile.getParentFile().exists()) {
                                // 如果目标文件所在的目录不存在，则创建父目录
                                objVideoFile.getParentFile().mkdirs();
                                log.info("**********创建上级目录成功**************");
                            }
                            multipartFile.transferTo(objVideoFile);
                            log.info("{----------------创建文件开结束------------------}");
                            log.info("**********上传成功**************");
                            // 如果是视频
                            if (flag.equals(suffixName.toLowerCase())) {
                                file.setFileName(fileName);
                                file.setFileSize(String.valueOf(multipartFile.getSize()));
                                log.info("{----------------视频文件路径-------------------}" + filePath);
                                file.setProjectFileUrl(filePath);// 视频文件路径
                                file.setSuffixName(suffixName.toLowerCase());
                            } else {
                                PicThumbnailUtils ptu = new PicThumbnailUtils(fileUrl);
                                String thumbnailUrl = null;
                                String thumbnailPath = File.separator + DateUtils.nowDayTimeStamp() + File.separator
                                        + uniqueImageName + File.separator + uuidImage + ".jpg";// 缩略图路径
                                log.info("{----------------缩略图路径-------------------}" + thumbnailPath);
                                if (PlatformUtils.isWindows()) {
                                    log.info("**********Windows**************");
                                    thumbnailUrl = CommonFileConstant.WINDOWS_FILE_URL + thumbnailPath;
                                } else if (PlatformUtils.isLinux()) {
                                    log.info("**********Linux**************");
                                    thumbnailUrl = CommonFileConstant.LINUX_FILE_URL + thumbnailPath;

                                }
                                log.info("{----------------缩略图URL-------------------}" + thumbnailUrl);
                                // 缩略图固定大小
                                ptu.resize(200, 200, thumbnailUrl);
                                file.setFileName(fileName);
                                file.setFileSize(String.valueOf(multipartFile.getSize()));
                                // 原图路径
                                log.info("{----------------图片路径-------------------}" + filePath);
                                file.setProjectFileUrl(filePath);
                                // 缩略图路径
                                log.info("{----------------缩略图路径-------------------}" + thumbnailPath);
                                file.setThumbnailUrl(thumbnailPath);
                                file.setSuffixName(suffixName);

                            }

                        } catch (IOException ie) {
                            log.error("------------上传异常--------------" + ie.getMessage());
                            return null;
                        } finally {
                            // 关闭FileOutputStream对象
                            try {
                                if (fos != null) {
                                    fos.close();
                                }
                            } catch (Exception e) {
                                e.getMessage();
                                e.printStackTrace();
                            }
                        }
                    }

                }

            }
            log.info("上传的文件:【" + file + "】");
            return file;
        }
        return file;
    }

    /**
     * @param request
     * @return
     * @throws Exception
     */
    public static UploadFileVO uploadImageOne(MultipartFile files) throws Exception {
        long startTime = System.currentTimeMillis();   //获取开始时间
        String filePath = "";
        String fileName = "";// 文件名称
        FileOutputStream fos = null;
        UploadFileVO file = null;
        String fileUrl = null;
      //  CommonsMultipartFile cf= (CommonsMultipartFile)files;
        //if (cf.isMultipart(multiRequest)) {
            log.info("{******************开始上传**********************}");
                        MultipartFile multipartFile=files;
                        file = new UploadFileVO();
                        fileName = multipartFile.getOriginalFilename();// 获取上传的文件名字
                        String uniqueImageName = RandomUtils.genImageName();
                        String uuidImage = RandomUtils.getUUID();
                        String suffixName = multipartFile.getOriginalFilename()
                                .substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1);
                        filePath = File.separator + DateUtils.nowDayTimeStamp() + File.separator + uniqueImageName
                                + File.separator + uniqueImageName + ".jpg";
                        if (multipartFile.getSize() > maxSize) {
                            log.info("{******************文件超过500M**********************}");
                            return null;
                        }
                        if (PlatformUtils.isWindows()) {
                            log.info("**********Windows**************");
                            fileUrl = CommonFileConstant.WINDOWS_FILE_URL + filePath;
                            log.info("**********fileUrl**************" + fileUrl);
                        } else if (PlatformUtils.isLinux()) {
                            log.info("**********Linux**************");
                            fileUrl = CommonFileConstant.LINUX_FILE_URL + filePath;
                            log.info("**********fileUrl**************" + fileUrl);

                        }
                        log.info("------------------文件创建开始----------------------");
                        try {
                            File objFile = new File(fileUrl); // 创建对象
                            if (!objFile.exists()) {
                                objFile.mkdir();

                            } else {
                                log.info(fileUrl + "====文件夹已存在");
                            }
                            // 上级文件是否存在
                            // 判断目标文件所在的目录是否存在
                            if (!objFile.getParentFile().exists()) {
                                // 如果目标文件所在的目录不存在，则创建父目录
                                objFile.getParentFile().mkdirs();
                            }
                            multipartFile.transferTo(objFile);
                            log.info("------------------文件创建结束----------------------");
                            log.info("**********上传成功**************");
                            PicThumbnailUtils ptu = new PicThumbnailUtils(fileUrl);
                            String thumbnailUrl = null;
                            String thumbnailPath = File.separator + DateUtils.nowDayTimeStamp() + File.separator
                                    + uniqueImageName + File.separator + uuidImage + ".jpg";
                            if (PlatformUtils.isWindows()) {
                                log.info("**********Windows**************");
                                thumbnailUrl = CommonFileConstant.WINDOWS_FILE_URL + thumbnailPath;
                            } else if (PlatformUtils.isLinux()) {
                                log.info("**********Linux**************");
                                thumbnailUrl = CommonFileConstant.LINUX_FILE_URL + thumbnailPath;

                            }
                            ptu.resize(200, 200, thumbnailUrl);
                            log.info("**********上传成功**************");
                            file.setFileName(fileName);
                            file.setFileSize(String.valueOf(multipartFile.getSize()));
                            file.setProjectFileUrl(filePath);// 原图路径
                            file.setThumbnailUrl(thumbnailPath);// 缩略图路径
                            file.setSuffixName(suffixName);
                        } catch (IOException ie) {
                            log.error("------------上传异常--------------" + ie.getMessage());
                            return file;
                        } finally {
                            // 关闭FileOutputStream对象
                            try {
                                if (fos != null) {
                                    fos.close();
                                }
                            } catch (Exception e) {
                                e.getMessage();
                                e.printStackTrace();
                            }
                        }
                   // }

               // }

            //}
            long endTime = System.currentTimeMillis(); //获取结束时间
            log.info("程序运行时间： " + (endTime - startTime) + "毫秒");
            log.info("上传的文件:【" + file + "】");
            return file;
        }



    /**
     * 文件类型判断
     *
     * @return
     */
    private static boolean checkFileType(String suffixName) {
        Iterator<String> type = Arrays.asList(allowFiles).iterator();
        while (type.hasNext()) {
            String ext = type.next();
            if (suffixName.toLowerCase().equals(ext)) {
                return true;
            }
        }
        return false;
    }


    /**
     * @Title: uploads
     * @Description:上传图片打点及水印
     * @return: UploadFileVo
     * @author:wgc
     * @Date: 2017年12月27日 @throws
     */
    public static UploadFileVO uploadImagByDOt(HttpServletRequest request, String lng,
                                               String lat) {
        long startTime = System.currentTimeMillis();   //获取开始时间
        String flag = null;//表示来自相册 不打经纬度
        if (StringUtils.isBlank(lng) || StringUtils.isBlank(lat)) {
            log.info("===========来自相册===========");
            flag = "0";
        } else {
            log.info("===========来自拍照或者离线===========");
            flag = "1";//表示来自拍照或离线图片 打上经纬度
        }
        String encoding = System.getProperty("file.encoding");
        log.info("Encoding:" + encoding);
        log.info("【经度】" + lng + "【,纬度】" + lat);
        // 日期 yyyy-MM-dd HH:mm:ss
        String waterMarkContent = DateUtils.getCurrentDateTimeToSeconds();
        UploadFileVO file = null;
        String waterMarkLng = "经度:" + lng;
        String waterMarkLat = "纬度:" + lat;
        log.info("【经度】" + waterMarkLng + "【纬度】" + waterMarkLat);
        FileOutputStream outImgStream = null;
        FileOutputStream outImgStreamWarter = null;
        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
                multiRequest.getSession().getServletContext());
        if (multipartResolver.isMultipart(multiRequest)) {
            Iterator<String> iter = multiRequest.getFileNames();
            while (iter.hasNext()) {
                List<MultipartFile> multipartFiles = multiRequest.getFiles(iter.next());
                if (multipartFiles != null && !multipartFiles.isEmpty()) {
                    for (MultipartFile multipartFile : multipartFiles) {
                        String suffix = multipartFile.getOriginalFilename()
                                .substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1);
                        log.info("**************图片类型*********************" + suffix);
                        file = new UploadFileVO();
                        String fileName = multipartFile.getOriginalFilename();// 获取上传的imag名字
                        String uniqueImageName = genImageName();// 生成唯一的图片名称
                        String uniqueImage = uniqueImageName;
                        BufferedImage bufImg;
                        String imagOriginalPath = null;
                        //返回给APP端
                        String imagOriginalUrl = File.separator + DateUtils.nowDayTimeStamp() + File.separator + uniqueImageName
                                + File.separator + "image" + File.separator + uniqueImage + ".jpg";
                        // 原图片路径
                        log.info("{**********原图片路径**************}" + imagOriginalUrl);
                        if (PlatformUtils.isWindows()) {
                            log.info("**********Windows**************");
                            imagOriginalPath = CommonFileConstant.WINDOWS_FILE_URL + imagOriginalUrl;
                            log.info("**********原图片存储路径**************" + imagOriginalPath);
                        } else if (PlatformUtils.isLinux()) {
                            log.info("**********Linux**************");
                            imagOriginalPath = CommonFileConstant.LINUX_FILE_URL + imagOriginalUrl;
                            log.info("**********原图片存储路径**************" + imagOriginalPath);

                        }

                        try {
                            File objFile = new File(imagOriginalPath); // 创建对象
                            if (!objFile.exists()) {
                                objFile.mkdir();

                            } else {
                                log.info(imagOriginalPath + "====文件夹已存在");
                            }
                            // 上级文件是否存在
                            // 判断目标文件所在的目录是否存在
                            if (!objFile.getParentFile().exists()) {
                                // 如果目标文件所在的目录不存在，则创建父目录
                                objFile.getParentFile().mkdirs();
                            }

                            // 得到图片的二进制数据，以二进制封装得到数据，具有通用性
                            byte[] data = readInputStream(multipartFile.getInputStream());
                            outImgStream = new FileOutputStream(objFile);
                            outImgStream.write(data);
                            PicThumbnailUtils ptu = new PicThumbnailUtils(imagOriginalPath);
                            String thumbnailUrl = null;
                            String thumbnailPath = File.separator + DateUtils.nowDayTimeStamp() + File.separator
                                    + uniqueImageName + File.separator + uniqueImage + ".jpg";
                            log.info("{**********缩略图路径**************}" + thumbnailPath);
                            if (PlatformUtils.isWindows()) {
                                log.info("**********Windows**************");
                                thumbnailUrl = CommonFileConstant.WINDOWS_FILE_URL + thumbnailPath;
                                log.info("********缩略图存储路径**************" + thumbnailPath);
                            } else if (PlatformUtils.isLinux()) {
                                log.info("**********Linux**************");
                                thumbnailUrl = CommonFileConstant.LINUX_FILE_URL + thumbnailPath;
                                log.info("********缩略图存储路径**************" + thumbnailPath);
                            }
                            ptu.resize(200, 200, thumbnailUrl);

                            String warterImagePath = null;
                            //返回给app端
                            String warterImageUrl = File.separator + DateUtils.nowDayTimeStamp() + File.separator + uniqueImageName
                                    + File.separator + "image" + File.separator + "warter" + File.separator
                                    + uniqueImage + ".jpg";
                            if (PlatformUtils.isWindows()) {
                                log.info("**********Windows**************");
                                log.info("**********warterImageUrl**************" + warterImageUrl);
                                warterImagePath = CommonFileConstant.WINDOWS_FILE_URL + warterImageUrl;
                                log.info("**********水印图片存储路径**************" + warterImagePath);
                            } else if (PlatformUtils.isLinux()) {
                                log.info("**********Linux**************");
                                log.info("**********warterImageUrl**************" + warterImageUrl);
                                warterImagePath = CommonFileConstant.LINUX_FILE_URL + warterImageUrl;
                                log.info("**********水印图片存储路径**************" + warterImagePath);
                            }

                            // 创建水印图片保存路径
                            File warterFile = new File(warterImagePath); // 创建对象
                            if (!warterFile.exists()) {
                                warterFile.mkdir();
                            }
                            // 上级文件是否存在
                            // 判断目标文件所在的目录是否存在
                            if (!warterFile.getParentFile().exists()) {
                                // 如果目标文件所在的目录不存在，则创建父目录
                                warterFile.getParentFile().mkdirs();
                            }
                            // File file=new File(mu);
                            Image srcImg = ImageIO.read(multipartFile.getInputStream());
                            int srcImgWidth = srcImg.getWidth(null);
                            int srcImgHeight = srcImg.getHeight(null);

                            bufImg = new BufferedImage(srcImgWidth, srcImgHeight,
                                    1);
                            Graphics2D g = bufImg.createGraphics();
                            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);

                            Font font = new Font("华文彩云", 3, srcImgWidth / 20);
                            g.setColor(Color.white);
                            float alpha = 0.8F;
                            g.setComposite(AlphaComposite.getInstance(10, alpha));
                            g.setFont(font);

                            int i = srcImgWidth * 14 / 20;
                            int z = srcImgHeight * 18 / 20;
                            int newy = srcImgHeight * 2 / 20;
                            int newz = srcImgHeight * 4 / 20;
                            log.info("======【日期:】=======" + waterMarkContent);
                            if ("0".equals(flag)) {
                                g.drawString("来自相册", i, z);
                            } else if ("1".equals(flag)) {
                                g.drawString(waterMarkLng, 10, newy);
                                g.drawString(waterMarkLat, 10, newz);
                                g.drawString(waterMarkContent, -1, z);
                            }

                            g.dispose();
                            // 上传一个put一个
                            // String resultImagPath = warterImagePath;
                            log.info(uniqueImage + fileName + "=================");
                            outImgStreamWarter = new FileOutputStream(warterFile);
                            ImageIO.write(bufImg, "jpg", outImgStreamWarter);
                            outImgStream.flush();
                            file.setFileName(fileName);
                            file.setFileSize(String.valueOf(multipartFile.getSize()));
                            file.setProjectFileUrl(imagOriginalUrl);// 原图路径
                            file.setWaterPath(warterImageUrl);//水印路径
                            file.setThumbnailUrl(thumbnailPath);// 缩略图路径
                            file.setSuffixName(suffix);
                        } catch (FileNotFoundException e) {
                            log.info(e.getMessage());
                        } catch (IOException e) {
                            log.info(e.getMessage());
                        } catch (Exception e) {
                            log.info(e.getMessage());
                        } finally {
                            try {
                                if (outImgStream != null) {
                                    outImgStream.close();
                                }
                            } catch (Exception e) {
                                e.getMessage();
                                e.printStackTrace();
                            }
                            try {
                                if (outImgStreamWarter != null) {
                                    outImgStreamWarter.close();
                                }
                            } catch (Exception e) {
                                e.getMessage();
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
            long endTime = System.currentTimeMillis(); //获取结束时间
            log.info("程序运行时间： " + (endTime - startTime) + "毫秒");
            log.info("上传的图片:【" + file + "】");
            return file;
        }
        return file;
    }

    /**
     * 图片名生成
     */
    public static String genImageName() {
        // 取当前时间的长整形值包含毫秒
        long millis = System.currentTimeMillis();
        // long millis = System.nanoTime();
        // 加上三位随机数
        Random random = new Random();
        int end3 = random.nextInt(999);
        // 如果不足三位前面补0
        String str = millis + String.format("%03d", end3);

        return str;
    }

    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        // 创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        // 每次读取的字符串长度，如果为-1，代表全部读取完毕
        int len = 0;
        // 使用一个输入流从buffer里把数据读取出来
        while ((len = inStream.read(buffer)) != -1) {
            // 用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        // 关闭输入流
        inStream.close();
        // 把outStream里的数据写入内存
        return outStream.toByteArray();
    }

    public String[] getAllowFiles() {
        return allowFiles;
    }

    public void setAllowFiles(String[] allowFiles) {
        this.allowFiles = allowFiles;
    }


}
